/* $Id: ActionLabel.java,v 1.2 2007/09/07 06:51:21 gigabass Exp $
 *
 * Copyright (c) Guido Ludwig. All rights reserved.
 * Use is subject to license terms.
 */
package de.guidoludwig.af;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

/**
 * (Very) simple implementation of a Label that delegates clicks to the action
 * performed method of an action.
 * 
 * @author Guido
 * 
 */
public class ActionLabel extends JLabel {

	private static final long serialVersionUID = 1L;
	private Action action;
	private Color def;
	private String defaultText;
	public ActionLabel(Action action) {
		super((String)action.getValue(Action.NAME));
		setIcon((Icon)action.getValue(Action.SMALL_ICON));
		this.action = action;
		this.addMouseListener(new ActionMouseAdapter());
		def = getForeground();
		defaultText = (String)action.getValue(Action.NAME);
	}

	private class ActionMouseAdapter extends MouseAdapter {
		@Override
		public void mouseClicked(MouseEvent evt) {
			if (!action.isEnabled()) {
				return;
			}
			setForeground(Color.LIGHT_GRAY);
			repaint();
			SwingUtilities.invokeLater(new ActionRunner());
		}
		
		@Override
		public void mouseEntered(MouseEvent evt) {
			setForeground(Color.BLUE);
			setText("<html><body><u>" + defaultText + "</u></body></html>");
			repaint();
		}
		@Override
		public void mouseExited(MouseEvent evt) {
			setForeground(def);
			setText(defaultText);
			repaint();
		}
	}
	
	private class ActionRunner implements Runnable {
		public void run() {
			action.actionPerformed(new ActionEvent(ActionLabel.this,
					ActionEvent.ACTION_PERFORMED, "Clicked"));
			setForeground(def);
			repaint();
		}
	}

	
//    private void activateLink() {
//        active = true;
//
//        linkColorSave = getForeground();
//        setForeground(linkColor);
//
//        backgroundColorSave = getBackground();
//        if (backgroundColor != null)
//            setBackground(backgroundColor);
//
//        setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));    
//        repaint();
//    }	
}
